//
//  AlertManager.m
//  EVETracker
//
//  Created by Scott A. Guyer on 8/8/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "AlertManager.h"


@implementation AlertManager

@synthesize repeatThreshold;


static AlertManager*   _sharedAlertMan = nil;



+ (AlertManager*) sharedAlertManager
{
	if ( _sharedAlertMan == nil )
	{
		_sharedAlertMan = [[AlertManager alloc] init];
	}
	
	return _sharedAlertMan;
}


- (id) init
{
	[super init];
	
	repeatThreshold = (NSTimeInterval) 5.0 ;
	
	// Initialize the instance
	_msgs = [[NSArray alloc] initWithObjects:
			 @"EVE Service Unavailable",
			 @"Data Is Current",
			 nil];
	
	_lastSeen = [[NSMutableArray alloc] initWithObjects:
				 [NSNull null],
				 [NSNull null],
				 nil];
	
	return self;
}






- (void) alert:(AlertManagerAlertType)type withString:(NSString*)msg  
{	
	
	NSDate* lastSeen = (NSDate*)[_lastSeen objectAtIndex:type];
	if ( ! [lastSeen isMemberOfClass:[NSNull class]] )
	{
		NSTimeInterval diff = [lastSeen timeIntervalSinceNow];
		// ^^^^ this does lastSeen - now, which will always be negative.
		// hence, the abs()
		if ( abs(diff) <= repeatThreshold )
			return;
	}
		
	[_lastSeen replaceObjectAtIndex:type withObject:[NSDate date]];
			
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[_msgs objectAtIndex:type]
													message:msg
												   delegate:nil 
										  cancelButtonTitle:@"OK" 
										  otherButtonTitles:nil];
	[alert show];
	[alert release];
}



@end
